package com.yiwei.zxp.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.api.model.ShopIndexModel;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class HomeBottomCateAdapter extends ItemAdapter<ShopIndexModel.DatasBean.CatsBean, HomeBottomCateAdapter.DefaultHolder> {

    private final int x30;
    private final int x26;
    private int poi = 0;

    public HomeBottomCateAdapter(Context context) {
        super(context);
        x30 = context.getResources().getDimensionPixelSize(R.dimen.x30);
        x26 = context.getResources().getDimensionPixelSize(R.dimen.x26);
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_home_bottom_cate, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        ShopIndexModel.DatasBean.CatsBean item = getItem(position);
        holder.txtName.setText(item.catname);
        if (poi == position) {
            holder.viewLine.setVisibility(View.VISIBLE);
            holder.txtName.setTextColor(0xffFF0030);
            holder.txtName.setTextSize(TypedValue.COMPLEX_UNIT_PX, x30);
            holder.txtName.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        } else {
            holder.viewLine.setVisibility(View.GONE);
            holder.txtName.setTextColor(0xff999999);
            holder.txtName.setTextSize(TypedValue.COMPLEX_UNIT_PX, x26);
            holder.txtName.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }
        holder.btnCate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onChooseListener != null) {
                    onChooseListener.onClick(item.id);
                }
                poi = position;
                notifyDataSetChanged();
            }
        });
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_name)
        TextView txtName;
        @BindView(R.id.btn_cate)
        RelativeLayout btnCate;
        @BindView(R.id.view_line)
        View viewLine;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setPoi(int poi) {
        this.poi = poi;
    }

    public int getSelectPoi() {
        return poi;
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String cate_id);
    }
}
